<?php

namespace Sharks\Core\Http\View;

use Sharks\Core\Http\Response;
use Sharks\Core\Http\Routing\RouteControl;
use Sharks\Core\Support\Base\Precondition;

/**
 * use example
 * 	new RouteView('Main@index');
 * or
 * 	new RouteView('Main@view/{id}', ['id'=>1]);
 * 
 */
class RouteView extends View {

	private $action;
	private $params;
	private $query;
	private $hash;

	public function __construct($action, $params, $query, $hash) {
		$this->action = $action;
		$this->params = $params;
		$this->query = $query;
		$this->hash = $hash;
	}

	public function param($name, $value) {
		$this->params[$name] = $value;
		return $this;
	}

	public function query($name, $value) {
		$this->query[$name] = $value;
		return $this;
	}

	/**
	 * 解析 查询字符串
	 */
	public function querys($queryString) {
//		array_merge($this->query, ());
		return $this;
	}

	public function hash($hash) {
		$this->hash = $hash;
		return $this;
	}

    public function render(Response $response) {
    	$url = RouteControl::url($this->action, $this->params, $this->query, $this->hash);
		Precondition::check(!empty($url), "参数不正确,无法导出正确的URL");
    	header('Location:' . $url);
    }
}